<template>
    <div class="user-info-form config-info-wrap">
        <el-tabs type="border-card">
            <el-tab-pane label="角色">
                <el-checkbox-group v-if="roleList.length > 0" v-model="selectRole">
                    <el-row v-for="item in roleList" :key="item.id">
                        <el-checkbox :label="item.id">{{item.name}}</el-checkbox>
                    </el-row>
                </el-checkbox-group>
            </el-tab-pane>
            <el-tab-pane label="首页卡片">
                <el-checkbox-group v-if="indexCardList.length > 0" v-model="selectIndexCard">
                    <el-row v-for="item in indexCardList" :key="item.id">
                        <template v-if="JSON.parse(item.dictValue).type == '2' || JSON.parse(item.dictValue).type == '1,2'">
                            <el-checkbox :label="JSON.parse(item.dictValue).id">{{JSON.parse(item.dictValue).name}}</el-checkbox>
                        </template>
                    </el-row>
                </el-checkbox-group>
            </el-tab-pane>
            <el-tab-pane label="报表">
                <el-checkbox-group v-if="reportList.length > 0" v-model="selectReport">
                    <el-row v-for="item in reportList" :key="item.id">
                        <template v-if="JSON.parse(item.dictValue).nodes != null">
                            <el-checkbox :label="JSON.parse(item.dictValue).id">{{JSON.parse(item.dictValue).name}}</el-checkbox>
                        </template>
                    </el-row>
                </el-checkbox-group>
            </el-tab-pane>
            <el-tab-pane label="消息">
                <el-tree
                    ref="messageTree"
                    :data="messageList"
                    :props="defaultProps"
                    show-checkbox
                    node-key="id"
                    :default-checked-keys="selectMessage">
                </el-tree>
            </el-tab-pane>
        </el-tabs>
        <el-row label-width="0" class="verifyidentity-button bottom-btns">
            <el-button @click.stop="closeFun">取消</el-button>
            <el-button type="primary" @click="saveConfigInfo">保存</el-button>
        </el-row>
    </div>
</template>
<script>
import { getDictByCode } from '@/api/dic'
import { getRoleAuthList } from '@/api/sysConfig'
import { saveObjectByOrgId, selectObjectByOrgId } from '@/api/org'

export default {
    name: 'ConfigInfoForm',
    props: {
        oldInfo: {
            type: Object
        },
        closeFun: {
            required: true
        }
    },
    data() {
        return {
            // 角色
            selectRole: [],
            roleList: [],
            // 首页卡片
            selectIndexCard: [],
            indexCardList: [],
            // 报表
            selectReport: [],
            reportList: [],
            // 消息
            selectMessage: [],
            messageList: [],
            defaultProps: {
                children: 'nodes',
                label: 'name',
                value: 'id'
            }
        }
    },
    created() {
        let that = this
        const MAPList = ['selectRole', 'selectMessage', 'selectReport', 'selectIndexCard']
        that.getUseRole()
        that.getIndexCard()
        that.getReport()
        that.getMessage()
        // 获取历史选择数据
        for (var i=0, len=MAPList.length; i < len; i++) {
            let index = i
            selectObjectByOrgId({
                type: index,
                orgId: that.oldInfo.organizationId
            }).then(res => {
                that[MAPList[index]] = res.data
            })
        }
    },
    methods: {
        getIndex (_arr,_obj) {
            var len = _arr.length;
            for(var i = 0; i < len; i++)
            {
                if(_arr[i] == _obj)
                {
                    return parseInt(i);
                }
            }
            return -1;
        },
        searchNodeIds(data, nodeIds, isNode) {
            if (data) {
                for (var i = 0; i < data.length; i++) {
                    if (null != data[i]) {
                        nodeIds.push({
                            'id' : data[i].id,
                            "name" : data[i].name,
                            "isNode":isNode
                        });
                        if (0 < data[i].nodes.length) {
                            this.searchNodeIds(data[i].nodes,nodeIds,1);
                        }
                    }
                }
            }
        },
        // 转换为树型结构数据
        resetTreeData(data) {
            let that = this
            let treeData = []
            let arr = data
            let operabilityArr = [].concat(arr)
            if (data) {
                for (var i = 0; i < arr.length; i++) {
                    let obj = JSON.parse(arr[i].dictValue)
                    // 父节点
                    if(obj.nodes && obj.nodes != '') {
                        let treeNode = {
                            id: obj.id,
                            name: obj.remark,
                            nodeIds: obj.nodes.split(','),
                            nodes: []
                        }
                        // 剔除已筛选的
                        if(that.getIndex(operabilityArr, arr[i]) > -1) {
                            operabilityArr.splice(that.getIndex(operabilityArr, arr[i]), 1)
                        }
                        for(var j = 0; j < treeNode.nodeIds.length; j++) {
                            for(var k = 0; k < arr.length; k++) {
                                if(arr[k].dictKey == treeNode.nodeIds[j]) {
                                    let nodeObj = JSON.parse(arr[k].dictValue)
                                    let childNode = {
                                        id: nodeObj.id,
                                        name: nodeObj.remark,
                                        nodeIds: '',
                                        nodes: []
                                    }
                                    // 剔除已筛选的
                                    if(that.getIndex(operabilityArr, arr[k]) > -1) {
                                        operabilityArr.splice(that.getIndex(operabilityArr, arr[k]), 1)
                                    }
                                    treeNode.nodes.push(childNode)
                                }
                            }
                        }
                        treeData.push(treeNode)
                    }
                }
                for(var m=0; m < operabilityArr.length; m++) {
                    let obj = JSON.parse(operabilityArr[m].dictValue)
                    let treeNode = {
                        id: obj.id,
                        name: obj.remark,
                        nodeIds: '',
                        nodes: []
                    }
                    treeData.push(treeNode)
                }
                return treeData
            }
        },
        // 获取角色
        getUseRole() {
            let that = this
            getRoleAuthList({
                orgId: that.oldInfo.organizationId
            }).then(res => {
                let nodeIds = []
                // that.searchNodeIds(res.data, nodeIds)
                that.roleList = res.data // nodeIds
            })
        },
        // 获取首页卡片
        getIndexCard() {
            let that = this
            getDictByCode({code: 48}).then(res => {
                that.indexCardList = res.data.dictValueObj
            })
        },
        // 获取报表
        getReport() {
            let that = this
            getDictByCode({code: 51}).then(res => {
                that.reportList = res.data.dictValueObj
            })
        },
        // 获取消息
        getMessage() {
            let that = this
            getDictByCode({code: 47}).then(res => {
                that.messageList = that.resetTreeData(res.data.dictValueObj)
            })
        },
        saveConfigInfo() {
            let that = this
            let parames = {
                id : that.oldInfo.organizationId,
                messageIds : that.$refs['messageTree'].getCheckedKeys().join(','),
                reportIds : that.selectReport.join(','),
                cardIds : that.selectIndexCard.join(','),
                roleIds : that.selectRole.join(',')
            }
            saveObjectByOrgId(parames).then(res => {
                if(res.status === 200) {
                    that.$message.success('保存成功')
                    that.closeFun()
                } else {
                    that.message.warning(res.info)
                }
            })
        }
    }
}
</script>
<style lang="scss">
.config-info-wrap {
    .bottom-btns {
        margin-top: 20px;
    }
}
</style>
